<?php

namespace app\common\model;

use think\Model;

class RebateRule extends Model
{
    // 表名
    protected $name = 'rebate_rule';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'type_text',
        'create_time_text',
        'update_time_text',
	    'grade_text'
    ];
    

    const TYPE_AMOUNT = 1;
    const TYPE_RATIO = 2;
    
    public static $_type = [
        self::TYPE_AMOUNT => '固定金额',
	    self::TYPE_RATIO => '比例'
    ];
    
    public function getTypeList()
    {
    	return self::$_type;
    }


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
	
	public function getGradeList()
	{
		return \app\admin\model\User::$_level;
	}
	
	
	public function getGradeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['grade'];
		$list = \app\admin\model\User::$_level;
		return isset($list[$value]) ? $list[$value] : '';
	}

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
